#if UNITY_EDITOR

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using LitJson;

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using Sirenix.OdinInspector;
using System.Xml.Serialization;
using Pangoo.Common;
using MetaTable;


namespace MetaTable
{
    [Serializable]
    public partial class UnitySceneDetailRowWrapper : MetaTableDetailRowWrapper<UnitySceneOverview, UnityUnitySceneRow>
    {
        [ShowInInspector]
        [Sirenix.OdinInspector.FilePath(Extensions = "unity", RequireExistingPath = true)]
        public string ScenePath
        {
            get
            {
                return UnityRow.Row.ScenePath;
            }
            set
            {
                UnityRow.Row.ScenePath = value;
                Save();
            }
        }


        SceneAsset m_SceneAsset;

        [ShowInInspector]
        public SceneAsset SceneAssetTarget
        {
            get
            {
                if (m_SceneAsset == null)
                {

                    m_SceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(ScenePath);
                }
                return m_SceneAsset;
            }
        }

        public enum UnitySceneType
        {
            [LabelText("美术场景")]
            Art,

            [LabelText("白模")]
            WhiteBox,

            [LabelText("动态配置")]
            DynamicConfig,

        }

        public UnitySceneType SceneType
        {
            get
            {
                return UnityRow.Row.SceneType.ToEnum<UnitySceneType>();
            }
            set
            {

            }
        }

        [Button("在编辑器打开")]
        public void OpenInEditor()
        {
            if (ScenePath.IsNullOrWhiteSpace() || !File.Exists(ScenePath)) return;

            var scene = EditorSceneUtility.GetHierarchyScene(ScenePath);
            if (scene == null)
            {
                EditorSceneManager.OpenScene(ScenePath, OpenSceneMode.Additive);
            }
        }



        [Button("在编辑器关闭")]
        public void CloseInEditor()
        {
            if (ScenePath.IsNullOrWhiteSpace()) return;
            var scene = EditorSceneUtility.GetHierarchyScene(ScenePath);
            if (scene != null)
            {
                EditorSceneManager.CloseScene(scene.Value, removeScene: true);
            }
        }

        [Button("加入build")]
        public void JoinBuild()
        {
            var newBuildingScene = new EditorBuildSettingsScene(ScenePath, true);
            var buildScenes = EditorBuildSettings.scenes;
            List<EditorBuildSettingsScene> buildSceneList = new List<EditorBuildSettingsScene>();
            foreach (var scene in buildScenes)
            {
                if (scene.path.Equals(ScenePath))
                {
                    return;
                }
                buildSceneList.Add(scene);
            }
            Debug.Log($"Add Scene:{newBuildingScene.path} To BuildSetting!");
            buildSceneList.Add(newBuildingScene);
            EditorBuildSettings.scenes = buildSceneList.ToArray();
            AssetDatabase.SaveAssets();
        }
    }
}
#endif

